package com.sl.eurekaserver.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sl.eurekaserver.entity.User;

/**
 * 测试合并请求的service
 * 
 * @author 舒莉
 *
 */
@Service
public class UserService {

	@Autowired
	private RestTemplate restTemplate;

	//100毫秒
	@HystrixCollapser(batchMethod="findAll", collapserProperties = {
			@HystrixProperty(name="timerDelayinMilliseconds", value="100")
	})
	public User find(String userName) {
		return restTemplate.getForObject("http://USER-SERVICE/users/{l} ",
				User.class, userName);
	}

	@SuppressWarnings("unchecked")
	@HystrixCommand
	public List<User> findAll(List<String> userNames) {
		return restTemplate.getForObject("http://USER-SERVICE/users?userNames={l} ",
				List.class, StringUtils.join(userNames, ", "));
	}
}
